package top.mcwebsite.lib.router.plugin

import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes

class RouteBuilderCodeClassVisitor(
    classWriter: ClassWriter,
    private val routeBuilderNames: Set<String>
) : ClassVisitor(Opcodes.ASM9, classWriter) {

    override fun visitMethod(
        access: Int,
        name: String?,
        descriptor: String?,
        signature: String?,
        exceptions: Array<out String>?
    ): MethodVisitor {
        println("router inject method name = $name")
        val oldVisitor = super.visitMethod(access, name, descriptor, signature, exceptions)
        return if (name == Constant.INJECT_METHOD) {
            println("matched route method")
            RouteBuilderInjectMethodVisitor(oldVisitor, routeBuilderNames, access, name, descriptor)
        } else {
            oldVisitor
        }
    }
}